package com.qf.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter("/*")

public class LoginFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse resp = (HttpServletResponse) response;
        HttpServletRequest req = (HttpServletRequest) request;
        String uri = req.getRequestURI();
        System.out.println("uri = " + uri);
        if (uri.contains("login") || uri.contains("logo.png") || uri.contains("jpg") || uri.contains("qf_login.html")) {
            chain.doFilter(request, response);
        } else {
            HttpSession session = req.getSession();
            String username = (String) session.getAttribute("username");
            if (username != null) {
                chain.doFilter(request, response);
            } else {
                resp.sendRedirect("/qf_login.html");
            }
        }
    }

    public void destroy() {

    }
}
